package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealDishService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId") //清理缓存
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        System.out.println(setmealDTO);
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        System.out.println(setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("状态")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true) //清理所有的缓存数据
    public Result setStatus(@PathVariable Integer status, Long id) {
        setmealService.setStatus(status, id);
        return Result.success();
    }

    @GetMapping("{id}")
    @ApiOperation("修改套餐回显")
    public Result<SetmealVO> toReviseSetmealInfo(@PathVariable Long id) {
        Setmeal seteaml = setmealService.getSeteamlById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(seteaml, setmealVO);
        List<SetmealDish> setmealDishList = setmealDishService.getSetmealDishById(id);
        setmealVO.setSetmealDishes(setmealDishList);
        return Result.success(setmealVO);
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true) //清理所有的缓存数据
    public Result reviseSetmealInfo(@RequestBody SetmealDTO setmealDTO) {
        setmealService.updateSetmealInfo(setmealDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除 and 批量删除")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true) //清理所有的缓存数据
    public Result removeSetmealByIds(@RequestParam List<Long> ids) {
        setmealService.removeSetmealByIds(ids);
        return Result.success();
    }

}
